package com.zblibrary.cainetwork.okhttp.network;

import android.util.Log;

import com.google.gson.JsonObject;
import com.zblibrary.cainetwork.okhttp.api.NetService;
import com.zblibrary.cainetwork.okhttp.bean.TestMapBean;
import com.zblibrary.cainetwork.okhttp.bean.TextPostMapBean;
import com.zblibrary.cainetwork.okhttp.bean.TextQuery;
import com.zblibrary.cainetwork.okhttp.bean.UploadPicBean;
import com.zblibrary.cainetwork.okhttp.utils.RetrofitUtils;

import java.io.File;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 描述 :观察者
 *
 * @作者 菜Android
 * @时间 2020年09月8日
 */
public class NetWorks extends RetrofitUtils {

    protected static final NetService service = getRetrofit().create(NetService.class);
    //设缓存有效期为1天
    protected static final long CACHE_STALE_SEC = 60 * 60 * 24 * 1;

    private static MultipartBody.Builder builder;

    /**
     * 插入观察者-泛型
     *
     * @param observable
     * @param observer
     * @param <T>
     */
    public static <T> void setSubscribe(Observable<T> observable, Observer<T> observer) {
        observable.subscribeOn(Schedulers.io())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    /**
     * 插入观察者-Object
     *
     * @param observable
     * @param observer
     */
    public static void setSubscribe2(Observable<Object> observable, Observer<Object> observer) {
        observable.subscribeOn(Schedulers.io())
                .subscribeOn(Schedulers.newThread())//子线程访问网络
                .observeOn(AndroidSchedulers.mainThread())//回调到主线程
                .subscribe(observer);
    }

    //Post请求
    public static RequestBody addAuthKeyToMapForPost2(JsonObject jsonObject) {
        String data = jsonObject.toString();
        // RequestBody requestBody = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"), data);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), data);
        return requestBody;
    }

    //上传图片
    public static MultipartBody addAuthKeyToMapForPost(String token, File file) {
        builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
        builder.addFormDataPart("token", token);
        Log.e("bk", "addAuthKeyToMapForPost: " + file.getName());
        try {
            if (file.exists()) {
                RequestBody imageBody = RequestBody.create(MediaType.parse("image/*"), file);
                builder.addFormDataPart("file", file.getName(), imageBody);
            }
        } catch (Exception e) {
            Log.e("Exception", "addAuthKeyToMapForPost: " + e.getMessage());
        }
        return builder.build();
    }


    // Query方式
    public static void GetTextQuery(String mobile, Observer<TextQuery> textQueryObserver) {
        setSubscribe(service.GetTextQuery(mobile), textQueryObserver);
    }

    //map 方式
    public static void GetQueryMap(Map<String, String> map, Observer<TestMapBean> testMapBeanObserver) {
        setSubscribe(service.GetQueryMap(map), testMapBeanObserver);
    }

    /**
     * post-map 方式(测试登陆接口)
     * 参数名字	类型	是否必须	默认值	其他	说明
     * mobile	整型	必填	-	-	手机号
     * pwd	整型	必填	-	-	密码
     */
    public static void PostTextPostMapBean(Map<String, String> map, Observer<TextPostMapBean> testMapBeanObserver) {
        setSubscribe(service.PostTextPostMapBean(map), testMapBeanObserver);
    }

    //上传文件
    public static void UploadPic(String token, File file, Observer<UploadPicBean> uploadPicBeanObserver) {
        setSubscribe(service.UploadPic(addAuthKeyToMapForPost(token, file)), uploadPicBeanObserver);
    }
}
